/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.touret.winecellar.reminder;

import info.touret.winecellar.bottle.server.domain.Bottle;
import info.touret.winecellar.userinfo.server.domain.UserInfo;

import java.io.IOException;
import java.net.URL;

import javax.inject.Inject;

import org.apache.log4j.Logger;

import com.google.appengine.api.utils.SystemProperty;
import com.google.appengine.api.utils.SystemProperty.Environment.Value;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.extensions.Reminder;
import com.google.gdata.data.extensions.Reminder.Method;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
	
/**
 * 
 * @author touret-a
 */
public class ReminderServiceImpl implements ReminderService {

	@Inject
	private CalendarService calendarService;

	public ReminderServiceImpl() {

	}

	@Override
	public void createEvent(Bottle bottle, UserInfo user)
			throws ReminderException {
		if (user == null || user.getGdatatoken() == null) {
			// test de l environnement
			if (SystemProperty.environment.value().equals(Value.Production)) {
				throw new ReminderException(
						"L'utilisateur est NULL ou le token n est pas defini");
			}
		}
		String sessionToken = user.getGdatatoken();
		trace.info("SESSION TOKEN utilise dans la transaction calendar :"+sessionToken);
		try {
			if (sessionToken != null) {

				URL postUrl = new URL("http://www.google.com/calendar/feeds/"
						+ user.getEmailAddress() + "/private/full");
				CalendarEventEntry myEntry = new CalendarEventEntry();
				myEntry.setTitle(new PlainTextConstruct(bottle.getName()));
				myEntry.setContent(new PlainTextConstruct(bottle.getName()
						+ " - " + bottle.getVintage() + " - "
						+ bottle.getDenomination()));
				DateTime startTime = new DateTime(bottle.getToDrink());
				DateTime endTime = new DateTime(bottle.getToDrink());
				When eventTimes = new When();
				eventTimes.setStartTime(startTime);
				eventTimes.setEndTime(endTime);
				myEntry.addTime(eventTimes);
				// ajout d une alerte
				addReminders(myEntry);
				trace.info("Insertion de l entree dans le calendrier google");
				CalendarEventEntry insertedEntry = calendarService.insert(
						postUrl, myEntry);
			}
		
		} catch (IOException ex) {
			trace.fatal(ex.getMessage(), ex);
			throw new ReminderException(ex.getLocalizedMessage());
		} catch (AuthenticationException ex) {
			trace.error(ex.getMessage(), ex);
			if (user != null) {
			}
			throw new ReminderException("L'utilisateur " + user
					+ " a un token invalide!\n Il va etre mis a NULL", ex);

		} catch (ServiceException ex) {
			trace.fatal(ex.getMessage(), ex);
			throw new ReminderException(ex.getLocalizedMessage());
		} catch (Exception ex) {
			trace.fatal(ex.getMessage(), ex);
			throw new ReminderException(ex.getLocalizedMessage());
		}
	}

	private static Logger trace = Logger.getLogger(ReminderServiceImpl.class);

	private void addReminders(CalendarEventEntry _entry) {
		final int DELAY = 1;
		Reminder alertReminder = new Reminder();
		alertReminder.setDays(DELAY);
		alertReminder.setMethod(Method.ALL);
		_entry.getReminder().add(alertReminder);
	}

}
